﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace QuizAppMvc2.Controllers
{
    public class MateriaController : Controller
    {
        //
        // GET: /Materia/
        quizdbEntities _db = new quizdbEntities();

        public ActionResult Index()
        {
            
            return View(_db.Materias);
        }

        //
        // GET: /Materia/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Materia/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Materia/Create

        [HttpPost]
        public ActionResult Create(Materia materia)
        {
            try
            {
                quizdbEntities db = new quizdbEntities();

                db.AddToMaterias(materia);
                db.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Materia/Edit/5
 
        public ActionResult Edit(int id)
        {
            var materiaToEdid = (from m in _db.Materias

                                   where m.idMateria == id

                                   select m).First();

            return View(materiaToEdid);
        }

        //
        // POST: /Materia/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, Materia editedMateria )
        {
            try
            {
                var materiaToEdid = (from m in _db.Materias

                                     where m.idMateria == id

                                     select m).First();

                _db.ApplyCurrentValues(materiaToEdid.EntityKey.EntitySetName, editedMateria);
                _db.SaveChanges();
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Materia/Delete/5
 
        public ActionResult Delete(int id)
        {
            var materiaToDelete = (from m in _db.Materias

                               where m.idMateria == id

                               select m).First();

            return View(materiaToDelete);
        }

        //
        // POST: /Materia/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, Materia materia )
        {
            try
            {
                var materiaToDelete = (from m in _db.Materias

                                       where m.idMateria == id

                                       select m).First();

                _db.DeleteObject(materiaToDelete);
                _db.SaveChanges();
 
                return RedirectToAction("Index");
            }
            catch (Exception ex )
            {
                return View();
            }
        }
    }
}
